package com.xinyue.game.gateway.server.socket;

import com.xinyue.game.gateway.common.GameGatewayMessageRequestHeader;
import com.xinyue.game.message.GameServerMessageModel;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author 王广帅
 * @Date 2021/5/30 19:33
 */
public class SocketDecoderHandler extends ChannelInboundHandlerAdapter {

    private Logger logger = LoggerFactory.getLogger(SocketDecoderHandler.class);


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof ByteBuf) {
            ByteBuf byteBuf = (ByteBuf) msg;
            GameGatewayMessageRequestHeader header = new GameGatewayMessageRequestHeader();
            byteBuf.skipBytes(4);
            header.readHeader(byteBuf);
            GameServerMessageModel<GameGatewayMessageRequestHeader> gameServerMessageModel = new GameServerMessageModel<>();
            gameServerMessageModel.setBody(byteBuf);
            gameServerMessageModel.setHeader(header);
            ctx.fireChannelRead(gameServerMessageModel);
        } else {
            ctx.fireChannelRead(msg);
        }
    }
}
